﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using woaC2.SocketLib;

namespace woaPoker.LogicLib.LogicCtrl
{
    public class PlayerRegistry
    {
        private PlayerRegistry()
        {
        }

        internal static PlayerRegistry instance
        {
            get
            {
                return Nested.instance;
            }
        }
        private class Nested
        {
            static Nested()
            {
            }
            internal static readonly PlayerRegistry instance = new PlayerRegistry();
        }

     
        public readonly ConcurrentDictionary<Guid,Player> listPlayer = new ConcurrentDictionary<Guid, Player>();

        //public List<Match> listMath1 = new List<Match>();
        
        internal int Count()
        {
            return this.listPlayer.Count();
           
        }
        internal void AddPlayer(Player player)
        {
            this.listPlayer.TryAdd(player.client.SessionID, player);
        }
        internal Player GetPlayer(Guid sessionId)
        {
            return this.listPlayer.Where(k => k.Value.client.SessionID == sessionId).Select(k => k.Value).SingleOrDefault();
        }
        internal void Remove(Guid sessionID)
        {
            try
            {
                if (listPlayer.ContainsKey(sessionID))
                {
                    Player val;
                    listPlayer.TryRemove(sessionID, out val);
                }
            }
            catch (ArgumentNullException ex)
            {
                SocketLog.Write("[MatchRegistry.Remove]--[ERROR]--" + ex);
            }
        }
    }
}
